<template>
  <div id="userRegisterPage">
    <div class="register-container">

      <div class="info-section">
        <div class="info-content">
          <div class="logo-section">
            <img src="/src/assets/logo.png" alt="Logo" class="logo">
            <h1 class="brand-name">AI应用生成</h1>
          </div>
          <h2 class="welcome-title">加入我们</h2>
          <p class="welcome-subtitle">注册您的账户，开启AI应用生成之旅</p>
          <div class="features">
            <div class="feature-item">
              <CheckCircleTwoTone /><span>免费注册使用</span>
            </div>
            <div class="feature-item">
              <CheckCircleTwoTone /><span>丰富的模板库</span>
            </div>
            <div class="feature-item">
              <CheckCircleTwoTone /><span>专业技术支持</span>
            </div>
          </div>
        </div>
      </div>

      <div class="form-section">
        <h2 class="title">用户注册</h2>
        <div class="desc">不写一行代码，生成完整应用</div>
        <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
          <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
            <a-input v-model:value="formState.userAccount" placeholder="请输入账号" size="large" />
          </a-form-item>
          <a-form-item name="userPassword" :rules="[
            { required: true, message: '请输入密码' },
            { min: 8, message: '密码不能小于 8 位' },
          ]">
            <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" size="large" />
          </a-form-item>
          <a-form-item name="checkPassword" :rules="[
            { required: true, message: '请确认密码' },
            { min: 8, message: '密码不能小于 8 位' },
            { validator: validateCheckPassword },
          ]">
            <a-input-password v-model:value="formState.checkPassword" placeholder="请确认密码" size="large" />
          </a-form-item>
          <div class="tips">
            已有账号？
            <RouterLink to="/user/login">去登录</RouterLink>
          </div>
          <a-form-item>
            <a-button type="primary" html-type="submit" style="width: 100%" size="large">注 册</a-button>
          </a-form-item>
        </a-form>
      </div>

    </div>
  </div>
</template>

<script setup lang="ts">
import { CheckCircleTwoTone } from '@ant-design/icons-vue';
import { useRouter } from 'vue-router'
import { userRegister } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import { reactive } from 'vue'

const router = useRouter()

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

/**
 * 验证确认密码
 * @param rule
 * @param value
 * @param callback
 */
const validateCheckPassword = (rule: unknown, value: string, callback: (error?: Error) => void) => {
  if (value && value !== formState.userPassword) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: API.UserRegisterRequest) => {
  const res = await userRegister(values)
  // 注册成功，跳转到登录页面
  if (res.data.code === 0) {
    message.success('注册成功')
    router.push({
      path: '/user/login',
      replace: true,
    })
  } else {
    message.error('注册失败，' + res.data.message)
  }
}
</script>

<style scoped>
#userRegisterPage {
  background: #f5f5f5;
  width: 100%;
  height: calc(100vh - 64px);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
}

.register-container {
  max-width: 900px;
  width: 100%;
  min-height: 460px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  display: flex;
}

.info-section {
  width: 50%;
  background-color: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px;
}

.info-content {
  text-align: center;
}

.logo-section {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 32px;
}

.logo {
  width: 48px;
  height: 48px;
  border-radius: 12px;
}

.brand-name {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  color: #262626;
}

.welcome-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 18px;
  color: #262626;
}

.welcome-subtitle {
  font-size: 15px;
  margin: 0 0 30px;
  color: #8c8c8c;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: #595959;
}

.feature-icon {
  color: #1890ff;
  font-size: 16px;
}

.form-section {
  width: 50%;
  padding: 48px;
  background: #fff;
}

.title {
  text-align: center;
  margin-bottom: 16px;
}

.desc {
  text-align: center;
  color: #bbb;
  margin-bottom: 16px;
}

.tips {
  margin-bottom: 16px;
  color: #bbb;
  font-size: 13px;
  text-align: right;
}
</style>
